use crate::runtime::response::MagicBoxResponse;
use crate::runtime::result::MagicBoxResult;
use crate::state;
use crate::utils::window;

#[tauri::command(rename_all = "snake_case")]
pub(crate) async fn invoke_active_window() -> MagicBoxResult<MagicBoxResponse<()>> {
    let window = state::application::application().window();
    if window.is_some() {
        let _ = window.unwrap().set_focus();

        return Ok(MagicBoxResponse::new_empty_success());
    }

    // 保留全局主窗口
    let window = window::build_main_window();
    if window.is_err() {
        log::error!("构建主窗口失败：{}", window.err().unwrap());

        return Ok(MagicBoxResponse::new_empty_success());
    }

    state::application::application_mut().set_window(Some(window.unwrap()));

    Ok(MagicBoxResponse::new_empty_success())
}